<?php
/**
 * Created by Command.
 * User: Vito
 */

namespace app\admin\curd\repair;

use app\common\curd\Index;
use app\model\Attachment;
use think\db\Query;
class RepairIndex extends Index
{
    /**
     * 关键词查询指定字段
     * @var string[]
     */
    protected $keywordQueryArgs = [];
    //追加标签数据
    protected $labelCallback = [
        'get_status_enum' => ['name' => '状态'],
    ];
    //查询条件
    protected $where = [];
    //字段
    protected $field = [];
    //排除字段
    protected $withoutField = false;
    //关联
    protected $with = [
        'user', 'repairUser', 'RpUser'
    ];
    /**
     * 需要查询条件的字段
     * 数据库字段和参数字段名不一样 'db_field' => 'param_field'
     * 数据库字段和参数字段名一样 'field'
     * @var array
     */
    protected $queryField = [];
    protected $middleware = ['middleWhere'];
    protected function formatMiddleware($next){

        $response = $next();
        
        // halt($respose);
        foreach ($response as $key => $value) {
            // print_r($value['fault_img']);
            // die;
            //   $fault_img = explode(",",$value['fault_img']);

            // $response[$key]['fault_list'] = [];
            $response[$key]['fault_list'] = $this->getImgList($value['fault_img']);

            $response[$key]['complete_list'] = $this->getImgList($value['complete_img']);
            
            // print_r($response[$key]['fault_url']['id']);
            // die;
        //   print_r(json_decode(Attachment::getImg($where)));
        //   die;
        }
        return $response;
    }
    /**
     * @Description: 获取故障图片组
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $value   图片组id
     * @Date: 2022-11-05 11:31:30
     * @version: 1.0.0
     * @return [type]
     */    
    protected function getImgList($value){
        $attachment = new Attachment;
        // $res = [];
        $where[] = ['id', 'in', $value];
        $res = json_decode($attachment->getImg($where),true);
        return $res;
    }
   
    protected function middleWhere($next)
    {
        $kw = $this->request['kw'];
        // halt($kw);
        if ($kw) {
            $this->field =
                [
                    'user.id' => 'building_id',
                    'users.id' => 'room_type_id',
                    // 'Room.building_number' => 'building_number',
                    'Repair.*'
                    // 'payRecord.*'
                ];
        }
        // if ($type == 'valid') {
        //     $this->field =
        //         [
        //             'Building.id' => 'building_id',
        //             'RoomType.id' => 'room_type_id',
        //             'Room.building_number' => 'building_number',
        //             'Room.*'
        //         ];
        // }

        return $next();
    }
    protected function queryCallback(Query $query)
    {
        // halt($this->request);
        $kw = $this->request['kw'];
        if ($kw) {
            $query
                ->join('wuye_user user', 'user.id = Repair.user_id', 'right')
                ->join('wuye_user users', 'users.id = Repair.repair_user_id', 'right')
                // ->join('wuye_building Building', 'Building.id = Room.building_number', 'right')
                // ->join('wuye_pay_record pr', 'pr.room_id = Room.id', 'right')
                ->where('user.real_name|users.real_name|Repair.location|Repair.content', 'like', '%' . $kw . '%');
            // ->whereOr('Building.name', 'like', '%' . $kw . '%')
            // ->whereOr('Room.name', 'like', '%' . $Room_name .'%')
            // ->whereOr('Building.building_number', 'like', '%' . $kw . '%')
            // ->whereOr('RoomType.name', 'like', '%' . $kw . '%');
        }
        // if ($type == 'valid') {
        //     // $where['RoomType.live_number'] = ['>', 'Room.use_people'];
        //     $query
        //         ->join('wuye_room_type RoomType', 'RoomType.id = Room.room_type', 'right')
        //         ->join('wuye_building Building', 'Building.id = Room.building_number', 'right')
        //         ->where('RoomType.live_number > Room.use_people');
        // }
    }
}